package com.yi.wood.gob.gate.cal;

import com.yi.wood.room.CoupleRoom;
import com.yi.wood.room.Step;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by moyi on 2018/10/29 10:15
 *
 * @author moyi
 */
public class GobCoupleRoom extends CoupleRoom implements Cal<GobCoupleRoom> {
    private static final Logger LOG = LoggerFactory.getLogger(GobCoupleRoom.class);


    private byte xs = 15;
    private byte ys = 15;
    /**
     * 无子：0
     * 黑子：1
     * 白子：2
     */
    private byte[][] gobMap = new byte[xs][ys];

    public void step(byte x, byte y, byte type) {
        if (gobMap[x][y] != 0) {
            LOG.info("{},{}已经落子", x, y);
            return;
        }
        gobMap[x][y] = type;
        Step step = new Step(x, y, type);
        getRecord().addFirst(step);
        boolean result = exeCal(this);
        LOG.info(">>>>>>>结束了吗={}", result);

        for (int i = 0; i < gobMap.length; i++) {
            for (int j = 0; j < gobMap[i].length; j++) {
                System.out.print(gobMap[i][j] + "\t");
            }
            System.out.println();
        }
    }

    @Override
    public boolean exeCal(GobCoupleRoom room) {
        Step step = getRecord().getFirst();
        byte x = step.getX();
        byte tx = x;
        byte y = step.getY();
        byte type = step.getType();
        byte ty = y;
        byte sum = 0;
        // 竖向，先向上边走，如果遇到反色后掉头
        for (byte i = x; i >= 0; i--) {
            if (gobMap[i][y] == type) {
                sum += type;
                x = i;
                if (sum == type*5){
                    break;
                }
            } else {
                break;
            }
        }
        sum = 0;
        for (byte j = x; j < xs; j++) {
            if (gobMap[j][y] == type) {
                sum += type;
                if (sum == type*5){
                    break;
                }
            } else {
                break;
            }
        }


        if (result(type, sum)) {
            return true;
        }

        // 还原坐标
        x = tx;
        y = ty;
        sum = 0;

        // 横向，先向左走，遇到反色掉头
        for (byte i = y; i >= 0; i--) {
            if (gobMap[x][i] == type) {
                sum += type;
                y = i;
                if (sum == type*5) {
                    break;
                }
            } else {
                break;
            }
        }
        sum = 0;
        for (byte j = y; j < ys; j++) {
            if (gobMap[x][j] == type) {
                sum += type;
                if (sum == type*5) {
                    break;
                }
            } else {
                break;
            }
        }

        if (result(type, sum)) {
            return true;
        }

        // 还原坐标
        x = tx;
        y = ty;
        sum = 0;

        // 撇向 /  先右上走，遇到反色掉头
        byte c = (byte) (x + y);
        for (byte i = x; i >= 0 && c-i < xs; i--) {
            if (gobMap[i][c - i] == type) {
                sum += type;
                x = i;
                if (sum == type*5) {
                    break;
                }

            } else {
                break;
            }
            if (i == c % 14) {
                break;
            }
        }
        sum = 0;
        for (int j = x; j < ys && c-j < ys; j++) {
            if (gobMap[j][c - j] == type) {
                sum += type;
                if (sum == type*5) {
                    break;
                }
            } else {
                break;
            }
            if (j == c % 14){
                break;
            }
        }

        if (result(type, sum)) {
            return true;
        }

        // 还原坐标
        x = tx;
        y = ty;
        sum = 0;

        // 捺向 \ 先向左上走，遇到反色掉头

        for (byte i = x,j=y; i >= 0 && j >= 0; i--,j--) {
            if (gobMap[i][j] == type) {
                sum += type;
                x = i;
                y = j;
                if (sum == type*5) {
                    break;
                }
            } else {
                break;
            }
        }
        sum = 0;
        for (byte n = x,m = y;n<xs && n<ys;n++,m++){
            if (gobMap[n][m] ==  type){
                sum += type;
                if (sum == type*5){
                    break;
                }
            }else {
                break;
            }
        }

        return result(type, sum);
    }

    private boolean result(byte type, byte sum) {
        int max = 5;
        if (type == 1 && sum == max) {
            return true;
        }
        return type == 2 && sum == 10;
    }

    public byte[][] getGobMap() {
        return gobMap;
    }

    public void setGobMap(byte[][] gobMap) {
        this.gobMap = gobMap;
    }

    public static void main(String[] args) {
        GobCoupleRoom room = new GobCoupleRoom();
        room.getGobMap()[14][14] = 1;
        room.getRecord().addLast(new Step((byte) 14,(byte)14,(byte)1));
        room.getGobMap()[13][13] = 1;
        room.getRecord().addLast(new Step((byte) 13,(byte)13,(byte)1));
        room.getGobMap()[12][12] = 1;
        room.getRecord().addLast(new Step((byte) 12,(byte)12,(byte)1));
        room.getGobMap()[11][11] = 1;
        room.getRecord().addLast(new Step((byte) 11,(byte)11,(byte)1));
        room.getGobMap()[10][10] = 1;
        room.getRecord().addLast(new Step((byte) 10,(byte)10,(byte)1));
        System.out.println(room.exeCal(room));

    }
}
